package com.eet.elasticsearch.controller;

import com.eet.elasticsearch.constants.CommonConstants;
import com.eet.elasticsearch.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 文本控制器
 *
 * @author zheng.zhang
 */
@RestController
public class ContentController {
    @Autowired
    private ContentService contentService;

    /**
     * 根据关键字批量新增到ES
     *
     * @param keyword 关键字
     * @return 是否成功
     * @throws IOException IO异常
     */
    @GetMapping("/parse/{keyword}")
    public ResponseEntity<Boolean> batchIncrease(@PathVariable("keyword") String keyword) throws IOException {
        return ResponseEntity.ok(contentService.addBatchContent(keyword));
    }

    /**
     * 查询内容
     *
     * @param keyword  关键字
     * @param pageNo   当前页
     * @param pageSize 页条数
     * @return 响应实体
     * @throws IOException IO异常
     */
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public ResponseEntity<List<Map<String, Object>>> searchContent(@PathVariable("keyword") String keyword,
                                                                   @PathVariable("pageNo") int pageNo,
                                                                   @PathVariable("pageSize") int pageSize) throws IOException {
        // 设置默认页码
        if (pageNo < 1) {
            pageNo = 1;
        }
        if (pageSize < 1) {
            pageSize = CommonConstants.DEFAULT_PAGE_SIZE;
        }
        return ResponseEntity.ok(contentService.searchContent(keyword, pageNo, pageSize));
    }
}
